/*
 * Created on Feb 29, 2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package ModuleBuilder.UI.tree;

import java.util.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.BorderFactory; 
import javax.swing.border.*;
import javax.swing.tree.*;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import ModuleBuilder.*;
import ModuleBuilder.model.*;	
import ModuleBuilder.UI.*;
import ModuleBuilder.UI.tree.UnitProfileEditor.AttributePanel;

import java.beans.*;

/**
 * @author Milton  Soong
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class StandPanel extends JPanel implements ActionListener {
	
	public static final int _SIMPLE_STAND =1;	// where profile applies to whole unit only
	public static final int _COMPLEX_STAND =2;	// where profile applies to whole unit only
	public static final int _MAX_NUM_SATNDS_IN_UNIT=36;
	public static final String TOP_VIEW_CMD="Pick Top View".intern();
	
	private static StandPanel loc;	// local instance used by the factory class
	
	// because there are multiple stand panels all subscribing to the Top Image Tray selection,
	// we need to differentiate which panel it is here
	//
	private static ImagePreview currImageSelector=null;
		
	JTextField nameTF;
	SpinnerNumberModel numStandsSPModel;
	ImageElement	gamePiecePix;
	ImagePreview figureTopView = null;
	JButton deleteB;	// delete stand button
	
	OBEditor editor;
	
	public StandPanel( OBEditor editorIn ) {
		deleteB = new JButton("Delete");
		editor = editorIn;
	}
	
	public static StandPanel create(int type, OBEditor editorIn) {
		StandPanel res=null;
		if ( type == _SIMPLE_STAND) {
			res = loc.new SimpleStandPanel(editorIn);
		}
		return ( res );
	}
	
	public void actionPerformed( ActionEvent e ){
		// Debug.out(this.toString() + ": actionPerformed() not yet implemented!");
		String command = e.getActionCommand();
    
		if (TOP_VIEW_CMD.equals(command)) {
			
			handleShowTopViewCmd();
		}
	}
	
	private void handleShowTopViewCmd() {
		showTray(TerrainTray._FIGURE_TOPVIEW, this.figureTopView);
	}
	
	public void showTray(String id, iImagePreviewChangeListener listener ) {
	  TerrainTray currTray = (TerrainTray)TerrainTray.lookup.get(id);
	  Dimension dlgSize = currTray.getPreferredSize();
	  Dimension frmSize = getSize();
	  Point loc = getLocation();
	  currTray.setSize(dlgSize);
	  // currTray.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
	  // currTray.pack();
	  currTray.setImagePreviewChangeListener(listener);
	  currTray.show();
	}
	
	public Stand store() {
		// to be overridden by subclass
		return ( null );
	}

	public static StandPanel load( Stand in, int type, OBEditor editorIn ){
		StandPanel res = StandPanel.create(type, editorIn);
		res.loadFromModel(in);
		return ( res );
	}
	
	protected void loadFromModel( Stand in ) {
		// to be overridden by subclass		
	}
	/**
	 * This class is used by a stand where a unit profile applies to the whole unit
	 * 
	 * @author Milton  Soong
	 *
	 * To change the template for this generated type comment go to
	 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
	 */
	public class SimpleStandPanel extends StandPanel {
		
		public SimpleStandPanel(OBEditor editorIn ) {
			super(editorIn);
			// init the input area
			JLabel nameL = new JLabel("Stand Name:");
			nameTF = new JTextField(); 	
			JPanel inputP = new JPanel();
			inputP.setLayout(new BoxLayout(inputP, BoxLayout.LINE_AXIS));
			inputP.add(nameL);
			inputP.add(nameTF);
		
			// init numof Stands area
			JLabel numStandL = new JLabel("Number of Stands:");
			numStandsSPModel = new SpinnerNumberModel(1, 1, _MAX_NUM_SATNDS_IN_UNIT, 1);
			JSpinner numStandSP = new JSpinner( numStandsSPModel );
			JPanel numStandP = new JPanel();
			numStandP.setLayout(new BoxLayout(numStandP, BoxLayout.LINE_AXIS));
			numStandP.add(numStandL);
			numStandP.add(numStandSP);
			
			// delete stand panel handling
			deleteB.addActionListener(new ActionListener()  {
				public void actionPerformed(ActionEvent e) {
					Object src = e.getSource();
					StandPanel sp = (StandPanel)editor.standPanelHash.get(src);
					if ( sp != null ){
						editor.standPanelHash.remove(src);
						editor.allStandsPanel.remove(sp);
						editor.stands.remove(sp);
						editor.validate();
					}
			}});			

			
			// Setup lefthand side panel
			JPanel leftP = new JPanel();
			leftP.setLayout(new BoxLayout(leftP, BoxLayout.PAGE_AXIS));
			leftP.add(inputP);
			leftP.add(numStandP);
			leftP.add(deleteB);
			
			// setup the Top view selection button
			JButton topViewButton = new JButton(TOP_VIEW_CMD);
			topViewButton.setActionCommand(TOP_VIEW_CMD);
			topViewButton.addActionListener(this);
			figureTopView = new ImagePreview(this, "Unit Top View");
			figureTopView.setAlignmentX(JComponent.LEFT_ALIGNMENT);
			figureTopView.setAlignmentY(JComponent.TOP_ALIGNMENT);
			JPanel middleP = new JPanel();
			middleP.setLayout(new BoxLayout(middleP, BoxLayout.PAGE_AXIS));
			figureTopView.setAlignmentX(JComponent.CENTER_ALIGNMENT);
			middleP.add(figureTopView);
			topViewButton.setAlignmentX(JComponent.CENTER_ALIGNMENT);
			middleP.add(topViewButton);		
			
			// now the final layout
			this.add(leftP);
			this.add(middleP);
			
			// now hook up the top view image picker
			TerrainTray topViewTray = TerrainTray.getTray(TerrainTray._FIGURE_TOPVIEW);
			// topViewTray.addImagePreviewChangeListener(figureTopView);
			return;
		}
		
		/**
		 * Load model content into a StandPanel
		 */
		protected void loadFromModel( Stand in ){
			String name;
			Integer numOfStands;	// number of identical stands in one unit
			ImageElement topImage;
			
			name = in.getName();
			numOfStands = in.getNumOfStands();
			topImage = in.getTopImage();
			
			nameTF.setText(name);
			numStandsSPModel.setValue(numOfStands);
			figureTopView.imageChanged(topImage);
			return;
		}
		
		/**
		 * Create a Stand data object from the UI
		 * @return Stand
		 */
		public Stand store() {
			Stand res = new Stand();
			String name = nameTF.getText();
			if (name == null) {
				name = "Default";
			}
			res.setName(name);
			Integer numStands = (Integer)numStandsSPModel.getNumber();
			res.setNumOfStands(numStands); 
			ImageElement el = figureTopView.getImageElement();
			res.setTopImage(el);
			return (res);
		}

	}
	
	/**
	 * @return Returns the deleteB.
	 */
	public JButton getDeleteB() {
		return deleteB;
	}
}
